package com.example.demo.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.example.demo.entity.SysUserExtra;
import com.example.demo.entity.dto.QWMessageDto;
import com.example.demo.mapper.SysUserExtraMapper;
import com.example.demo.service.ThirdPartyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: lu
 * @Date: 2024-08-26-16:17
 * @Description:
 */
@Service
public class ThirdPartyServiceImpl implements ThirdPartyService {
    @Value("${device.qywx.corpid}")
    private String corpid;
    @Value("${device.qywx.corpsecret}")
    private String corpsecret;
    @Value("${device.qywx.tokenUrl}")
    private String tokenUrl;
    @Value("${device.qywx.messageUrl}")
    private String messageUrl;
    @Value("${device.qywx.authorizeUrl}")
    private String authorizeUrl;
    @Value("${device.qywx.getuserinfo}")
    private String getuserinfo;


    private static final String QW_TOKEN_KEY = "conch:device:qywx:token";
    private static final String QW_MESSAGE_IDS = "conch:device:qywx:message:";

    @Autowired
    SysUserExtraMapper sysUserExtraMapper;

    //查询企微账号
    private String queryQwUserid(List<Integer> userIds){
        String qwUserids = "";
        if(userIds!=null&&!userIds.isEmpty()){
            for (Integer userId : userIds) {
                Map<String,Object> sysUserExtra = new HashMap<>();
                sysUserExtra.put("user_id", userId);
                List<SysUserExtra> extraUser = sysUserExtraMapper.selectByMap(sysUserExtra);
                if(extraUser!=null&&!extraUser.isEmpty()){
                    qwUserids = qwUserids + extraUser.get(0).getValue() + "|";
                }
            }
        }
        //if(!StringUtils.isBlank(qwUserids)){
            qwUserids = qwUserids.substring(0,qwUserids.length()-1);
        //}
        return qwUserids;
    }


    //
    //public String qyWXAccessToken(){
    //    String accessToken= Convert.toStr(redisUtil.get(QW_TOKEN_KEY));
    //    if(StringUtils.isNoneBlank(accessToken)){
    //        return accessToken;
    //    }
    //    String url = String.format(tokenUrl+"?corpid=%s&corpsecret=%s", corpid, corpsecret);
    //    String res = restTemplateUtil.get(url ,accessToken);
    //    JSONObject obj = JSONObject.parseObject(res);
    //    if(obj.getInteger("errcode").intValue() == 0){
    //        accessToken = obj.getString("access_token");
    //        Integer expiresIn = obj.getInteger("expires_in");
    //        redisUtil.set(QW_TOKEN_KEY, accessToken, expiresIn);
    //    }
    //    return accessToken;
    //}



    /**
     * 以文本形式发送至企微
     * @param vo
     * @return
     * @throws Exception
     */
    @Override
    public String sendQyWxMessageOnText(QWMessageDto vo) throws Exception {
        String userids = "";
        if(vo.getUserids()!=null){
            userids = this.queryQwUserid(vo.getUserids());
        }
        if(StringUtils.isBlank(userids)){
            return "未查询到企业微信账号";
        }



        return null;
    }
}
